﻿Imports MySql.Data.MySqlClient

Public Class frmproductdetail
    Public NewEntry_State As Boolean
    Dim Strsql As String
    Dim mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Dim DA As New MySqlDataAdapter
    Dim DAUnit As New MySqlDataAdapter
    Dim BDS As New BindingSource
    Dim BDSUnit As New BindingSource
    Dim commandBuilder As New MySqlCommandBuilder
    Dim DSWHIS As New DSWHIS
#Region "Function"
    Sub Load_Site()
        Dim Col_Site As New DataGridViewComboBoxColumn
        Dim Col_Item As New DataGridViewTextBoxColumn
        Dim Col_MinStockQty As New DataGridViewTextBoxColumn
        Dim Col_MaxStockQty As New DataGridViewTextBoxColumn
        Dim Col_MinReOrderQty As New DataGridViewTextBoxColumn
        Dim Col_MaxReOrderQty As New DataGridViewTextBoxColumn
        Dim Col_MinThermo As New DataGridViewTextBoxColumn
        Dim Col_MaxThermo As New DataGridViewTextBoxColumn

        With Col_Site
            .HeaderText = "คลัง"
            .Width = 200
            Strsql = "select siteid,site from site  order by site"
            Dim DTSite As New DataTable
            DTSite = mysql.GetMYSQLDataTable(Strsql, "site")
            .DataSource = DTSite
            .DisplayMember = "site"
            .ValueMember = "siteid"
            .DataPropertyName = "site"
        End With

        With Col_Item
            .Visible = False
            .DataPropertyName = "itemnumber"

        End With


        With Col_MinStockQty
            .HeaderText = "จำนวนต่ำสุด"
            .Width = 100
            .DataPropertyName = "minstockquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        End With

        With Col_MaxStockQty
            .HeaderText = "จำนวนมากสุด"
            .Width = 100
            .DataPropertyName = "maxstockquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MinReOrderQty
            .HeaderText = "ซื้ออย่างน้อย"
            .Width = 100
            .DataPropertyName = "reordermaxquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MaxReOrderQty
            .HeaderText = "ซื้อได้ไม่เกิน"
            .Width = 100
            .DataPropertyName = "reorderminquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MinThermo
            .HeaderText = "อุณหภูมิต่ำสุด"
            .Width = 100
            .DataPropertyName = "thermo_low"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MaxThermo
            .HeaderText = "อุณหภูมิสูงสุด"
            .Width = 100
            .DataPropertyName = "thermo_hight"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With GridStock
            '.Columns.Add(Col_SiteID)
            Strsql = "Select * from siteitem where itemnumber = " & Me.TxtItemNumber.Text
            Dim DTStock As New DataTable
            DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
            DTStock = DSWHIS.Tables("siteitem")
            DA.Fill(DTStock)
            BDS.DataSource = DTStock
            commandBuilder = New MySqlCommandBuilder(DA)
            .AutoGenerateColumns = False
            .DataSource = BDS
            .Columns.Add(Col_Site)
            .Columns.Add(Col_Item)
            .Columns.Add(Col_MinStockQty)
            .Columns.Add(Col_MaxStockQty)
            .Columns.Add(Col_MinReOrderQty)
            .Columns.Add(Col_MaxReOrderQty)
            .Columns.Add(Col_MinThermo)
            .Columns.Add(Col_MaxThermo)
        End With
    End Sub
    Sub Load_itemcontainunit()

        Dim Col_Item As New DataGridViewTextBoxColumn
        Dim Col_containunit As New DataGridViewTextBoxColumn
        Dim Col_qtyperunit As New DataGridViewTextBoxColumn
        Dim Col_note As New DataGridViewTextBoxColumn
        Dim Col_active As New DataGridViewCheckBoxColumn
        Dim Col_StandardPrice As New DataGridViewTextBoxColumn

        With Col_Item
            .Visible = False
            .DataPropertyName = "itemid"
        End With

        With Col_containunit
            .HeaderText = "หน่วยบรรจุ"
            .Width = 100
            .DataPropertyName = "containunit"
        End With

        With Col_qtyperunit
            .HeaderText = "จำนวนบรรจุ"
            .Width = 100
            .DataPropertyName = "qtyperunit"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_StandardPrice
            .HeaderText = "ราคากลาง"
            .Width = 100
            .DataPropertyName = "standardprice"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_active
            .HeaderText = "เปิดใช้งาน"
            '.Width = 100
            .DataPropertyName = "active"

        End With

        With Col_note
            .HeaderText = "note"
            .Width = 100
            .DataPropertyName = "หมายเหตุ"
        End With


        Strsql = "Select itemid,containunit,qtyperunit,standardprice,note,active from itemcontainunit where itemid = " & Me.TxtItemNumber.Text
        Dim DTcontainunit As New DataTable
        DAUnit = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
        DTcontainunit = DSWHIS.Tables("itemcontainunit")
        DAUnit.Fill(DTcontainunit)
        BDSUnit.DataSource = DTcontainunit
        commandBuilder = New MySqlCommandBuilder(DAUnit)

        With Gridcontainunit
            .AutoGenerateColumns = False
            .DataSource = BDSUnit
            .Columns.Add(Col_Item)
            .Columns.Add(Col_containunit)
            .Columns.Add(Col_qtyperunit)
            .Columns.Add(Col_StandardPrice)
            .Columns.Add(Col_active)
            .Columns.Add(Col_note)
        End With

    End Sub
    Sub Load_ProductGroup()
        Strsql = "Select section_id,section_name from section order by readonly desc,section_name"
        Dim DTSection As New DataTable
        DTSection = mysql.GetMYSQLDataTable(Strsql, "section")
        With Me.Cbosection
            .DataSource = DTSection
            .DisplayMember = "section_name"
            .ValueMember = "section_id"
        End With
    End Sub
    Sub Load_ProductType(Optional ByVal ProductGroup As Integer = 0)
        Strsql = "Select Category_id,Category_name from category where sectionid = " & ProductGroup & " order by readonly desc,Category_name"
        Dim DTcategory As New DataTable
        DTcategory = mysql.GetMYSQLDataTable(Strsql, "category")
        With Me.CboCategory
            .DataSource = DTcategory
            .DisplayMember = "Category_name"
            .ValueMember = "Category_id"
        End With
    End Sub
    Sub Load_ItemData()
        On_load = True
        Strsql = "Select * from item where itemnumber = " & Me.TxtItemNumber.Text
        Dim DTItem As New DataTable
        DTItem = mysql.GetMYSQLDataTable(Strsql, "item")
        Me.txtitemname.Text = DTItem.Rows(0).Item("itemname").ToString
        Me.TxtImgpart.Text = DTItem.Rows(0).Item("itemimage").ToString
        Me.txtuom.Text = DTItem.Rows(0).Item("uom").ToString
        Me.txtstandNumber.Text = DTItem.Rows(0).Item("standnumber").ToString
        Me.txtLocalnumber.Text = DTItem.Rows(0).Item("localnumber").ToString
        Me.TxtBarcode.Text = DTItem.Rows(0).Item("alternatenumber").ToString
        Me.TxtDesc.Text = DTItem.Rows(0).Item("note").ToString
        Me.NUDStandardprice.Value = DTItem.Rows(0).Item("standdardprice")
        Me.NUDCostPrice.Value = DTItem.Rows(0).Item("cost")
        Me.Cbosection.SelectedValue = DTItem.Rows(0).Item("section")
        Load_ProductType(Me.Cbosection.SelectedValue)
        Me.CboCategory.SelectedValue = DTItem.Rows(0).Item("category")
        Me.ChkActive.Checked = DTItem.Rows(0).Item("active")
        If IsDBNull(DTItem.Rows(0).Item("registerdate")) = True Then
            Me.DTPDateRegister.Value = Now
        Else
            Me.DTPDateRegister.Value = DTItem.Rows(0).Item("registerdate")
        End If
        On_load = False

    End Sub
    Function Validate_Data() As Boolean
        Validate_Data = False
        If Me.txtitemname.Text = "" Then
            MsgBox("กรุณาใส่ชื่อวัสดุ", MsgBoxStyle.Exclamation)
            Exit Function
        End If



        If Me.txtuom.Text = "" Then
            MsgBox("กรุณาใส่หน่วยนับ", MsgBoxStyle.Exclamation)
            Exit Function
        End If

        'If Me.Gridcontainunit.RowCount <= 1 Then
        '    MsgBox("กรุณาใส่หน่วยบรรจุอย่างน้อย 1 ตัว", MsgBoxStyle.Exclamation)
        'End If

        'If Me.GridStock.RowCount <= 1 Then
        '    MsgBox("กรุณาเลือกคลังจัดเก็บอย่างน้อย 1 คลัง", MsgBoxStyle.Exclamation)
        'End If
        Validate_Data = True
        Return Validate_Data

    End Function

#End Region


    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub
    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click

    End Sub

    Private Sub PicLoadImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicLoadImg.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .ShowDialog()

                Me.TxtImgpart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImgpart.Text)
                PictureBox1.Image = bm
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Frmcapture.ShowDialog()
        Me.TxtImgpart.Text = Frmcapture.MyCapturepart
        Frmcapture.Dispose()
    End Sub

    Private Sub picZoomin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picZoomin.Click
        FrmFullSize.PictureBox1.Image = Me.PictureBox1.Image
        FrmFullSize.Show()
    End Sub

    Private Sub frmproductdetail_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

    End Sub

    Private Sub frmproductdetail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Load_ProductGroup()
        Load_ProductType(Me.Cbosection.SelectedValue)

        If Me.NewEntry_State = False Then
            Load_ItemData()
            Load_Site()
            Load_itemcontainunit()
            Me.BtnDelete.Enabled = True
        End If

        On_load = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Validate_Data() = False Then Exit Sub
        If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่ ?:", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then Exit Sub
        Select Case NewEntry_State
            Case True
                'Insert Table Item
                Strsql = "Insert into item (registerdate,itemname,alternatenumber,section,category,itemimage,standdardprice,cost,standnumber,localnumber,uom,note,active) "
                Strsql = Strsql & " values('" & Pn_Framework.MysqlDateTimeFormat(Me.DTPDateRegister.Value, False) & "','" & Me.txtitemname.Text & "','" & Me.TxtBarcode.Text & "'," & Me.Cbosection.SelectedValue & "," & Me.CboCategory.SelectedValue
                Strsql = Strsql & ",'" & Me.TxtImgpart.Text & "'," & Me.NUDStandardprice.Value & "," & Me.NUDCostPrice.Value & ",'" & Me.txtstandNumber.Text & "','" & Me.txtLocalnumber.Text
                Strsql = Strsql & "','" & Me.txtuom.Text & "','" & Me.TxtDesc.Text & "'," & Me.ChkActive.Checked & ")"
                mysql.MySQLExecute(Strsql)
                NewEntry_State = False
                Me.BtnDelete.Enabled = True

                Strsql = "Select itemnumber from item order by itemnumber desc limit 1"
                Me.TxtItemNumber.Text = mysql.MySQLExecuteScalar(Strsql)
                Load_Site()
                Load_itemcontainunit()
            Case False
                Strsql = "Update item set registerdate='" & Pn_Framework.MysqlDateTimeFormat(Me.DTPDateRegister.Value, False) & "',itemname= '" & Me.txtitemname.Text & "',alternatenumber= '" & Me.TxtBarcode.Text & "',section=" & Me.Cbosection.SelectedValue
                Strsql = Strsql & ",category=" & Me.CboCategory.SelectedValue & ",itemimage= '" & Me.TxtImgpart.Text & "',standdardprice=" & Me.NUDStandardprice.Value & ",cost=" & Me.NUDCostPrice.Value
                Strsql = Strsql & ",standnumber= '" & Me.txtstandNumber.Text & "',localnumber= '" & Me.txtLocalnumber.Text & "',uom= '" & Me.txtuom.Text & "',note= '" & Me.TxtDesc.Text
                Strsql = Strsql & "',active=" & Me.ChkActive.Checked & " where itemnumber = " & Me.TxtItemNumber.Text
                mysql.MySQLExecute(Strsql)

                DA.Update(CType(BDS.DataSource, DataTable))
                DAUnit.Update(CType(BDSUnit.DataSource, DataTable))
        End Select
        MsgBox("บันทึกข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
        frmproductlist.Load_Data()

    End Sub
    Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            If Me.TxtBarcode.Text.Trim.Length = 0 Then Exit Sub
            'BarCodeCtrl1.Print()
            Dim MyBarcode As New Cls_Barcode
            If DlgTotalPage.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
            MyBarcode.Print_Barcode(Me.TxtBarcode.Text, DlgTotalPage.NUDTotalPage.Value)
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
    Private Sub GridStock_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles GridStock.DefaultValuesNeeded
        With Me.GridStock
            .Rows(e.Row.Index).Cells(1).Value = Me.TxtItemNumber.Text
        End With
    End Sub
    Private Sub Cbosection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbosection.SelectedIndexChanged
        If On_load = True Then Exit Sub
        Load_ProductType(Me.Cbosection.SelectedValue)
    End Sub

    Private Sub Gridcontainunit_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles Gridcontainunit.DefaultValuesNeeded
        With Me.Gridcontainunit
            .Rows(e.Row.Index).Cells(0).Value = Me.TxtItemNumber.Text
            .Rows(e.Row.Index).Cells(2).Value = 1
            .Rows(e.Row.Index).Cells(3).Value = True
        End With
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub Gridcontainunit_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Gridcontainunit.CellContentClick

    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("คุณต้องการลบข้อมูลตัวนี้ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

        Strsql = "Delete from item where itemnumber = " & Me.TxtItemNumber.Text
        mysql.MySQLExecute(Strsql)

        Strsql = "Delete from siteitem where itemnumber = " & Me.TxtItemNumber.Text
        mysql.MySQLExecute(Strsql)

        Strsql = "Delete from itemcontainunit where itemid = " & Me.TxtItemNumber.Text
        mysql.MySQLExecute(Strsql)

        MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
        frmproductlist.Load_Data()
        Me.Dispose()
    End Sub
End Class